<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function quickSort(arr){
        const {length} = arr

        // 终止递归条件
        if(length < 2){
          return arr
        }

        // 基准值
        let base = arr[0]

        // 拆分以基准值为大小的数组
        let minArr = arr.slice(1).filter(item => item <= base)
        let maxArr = arr.slice(1).filter(item => item > base)

        // 递归调用 并合并数组
        return quickSort(minArr).concat(base).concat(quickSort(maxArr))
      }
      
      const res = quickSort([3,4,5,2,1])
      console.log(res);
    </script>
  </body>
</html>
